package com.ly.hotel.spainterface.jg.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.jg.bean.gson.bill.SelfConsumeRecord;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ThinkPad on 2018/3/13.
 */

public class MassageBillAdapter extends BaseAdapter {

    private Context context;
    private List<SelfConsumeRecord> selfConsumeRecords;

    public MassageBillAdapter(Context context,List<SelfConsumeRecord> selfConsumeRecords){
        this.context = context;
        this.selfConsumeRecords = selfConsumeRecords;
    }

    @Override
    public int getCount() {
        return selfConsumeRecords.size();
    }

    @Override
    public Object getItem(int position) {
        return selfConsumeRecords.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.jg_listview_bill_item,null);
            viewHolder = new ViewHolder();
            viewHolder.goodsName = view.findViewById(R.id.goods_name);
            viewHolder.goodsPrice = view.findViewById(R.id.goods_price);
            viewHolder.goodsUnit = view.findViewById(R.id.goods_unit);
            viewHolder.goodsNum = view.findViewById(R.id.goods_num);
            viewHolder.goodsInputTimes = view.findViewById(R.id.goods_input_times);
            viewHolder.goodsPriceTotal = view.findViewById(R.id.goods_price_total);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        SelfConsumeRecord selfConsumeRecord = selfConsumeRecords.get(position);
        double price = selfConsumeRecord.getPrice();
        int numbercount = selfConsumeRecord.getNumbercount();
        String name = selfConsumeRecord.getGoodsname();
        double money = selfConsumeRecord.getMoney();
        String oldkeyid = selfConsumeRecord.getOldkeyid();//手牌号
        String startTime = selfConsumeRecord.getStarttime();
        String TempMemberid = selfConsumeRecord.getTempMemberid();
        startTime = currentTime(startTime);

        viewHolder.goodsName.setText(name);
        viewHolder.goodsPrice.setText(price + "");
        viewHolder.goodsUnit.setText(TempMemberid);
        viewHolder.goodsNum.setText(numbercount + "");
        viewHolder.goodsInputTimes.setText(startTime);
        viewHolder.goodsPriceTotal.setText(money + "");
        return view;
    }

    class ViewHolder{
        TextView goodsName;
        TextView goodsPrice;
        TextView goodsUnit;
        TextView goodsNum;
        TextView goodsInputTimes;
        TextView goodsPriceTotal;
    }

    public String currentTime(String time){
        time = time.replace('T',' ');
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date date = df.parse(time);
            time = df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Log.d("JgActivity",time);
        return time;
    }
}
